//
//	 ______    ______    ______    
//	/\  __ \  /\  ___\  /\  ___\   
//	\ \  __<  \ \  __\_ \ \  __\_ 
//	 \ \_____\ \ \_____\ \ \_____\ 
//	  \/_____/  \/_____/  \/_____/ 
//
//	Powered by BeeFramework
//
//
//  EditPetsDocument.m
//  DingDing
//
//  Created by LHL on 14/11/24.
//  Copyright (c) 2014年 com.gordon. All rights reserved.
//

#import "EditPetsDocument.h"
#import "ChoosePets.h"
#import "updatePetCell.h"
#import "Common.h"
#pragma mark -

@interface EditPetsDocument()
{
	//<#@private var#>
}
@end

@implementation EditPetsDocument{
    UIButton *rightbutton;
    BOOL isEdit;
    CustomDatePiker *piker; //自定义日期控件
    UIImagePickerController *imagePicker; //选择头像
    PETMODEL *currentModel;
    NSInteger newage;
}

SUPPORT_AUTOMATIC_LAYOUT( YES )
SUPPORT_RESOURCE_LOADING( YES )
DEF_OUTLET(BeeUIScrollView, list)
DEF_MODEL(updatePetInfo, iupdatePetInfo)
DEF_MODEL(updatePetHead, iupdatePetHead)
DEF_MODEL(getPetInfoByID, igetPetInfoByID)
- (void)load
{
    self.iupdatePetHead = [updatePetHead modelWithObserver:self];
    self.igetPetInfoByID = [getPetInfoByID modelWithObserver:self];
    self.iupdatePetInfo = [updatePetInfo modelWithObserver:self];
}

- (void)unload
{
}


ON_SIGNAL3(getPetInfoByID, RELOADING, SIGNAL){
    [self presentLoadingTips:@"加载中"];
}

ON_SIGNAL3(getPetInfoByID, RELOADED, SIGNAL){
    [self dismissTips];
    self.iupdatePetHead.pet_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"pet_id"];
    [self.list reloadData];
}

ON_SIGNAL3(updatePetHead, RELOADED, SIGNAL){
    [self dismissTips];
    [self presentMessageTips:@"更新成功"];
}

ON_SIGNAL3(updatePetHead, RELOADING, SIGNAL){

    [self presentLoadingTips:@"更新中"];
}

ON_SIGNAL3(updatePetInfo, RELOADED, SIGNAL){
    [self dismissTips];
    if ([self.iupdatePetInfo.code isEqualToString:@"200"]) {
        [self presentMessageTips:@"更新成功"];
        [[NSUserDefaults standardUserDefaults]setObject:[NSString stringWithFormat:@"%d",newage] forKey:@"age"];
        [[NSUserDefaults standardUserDefaults]setObject:[NSString stringWithFormat:@"%@",self.iupdatePetInfo.age] forKey:@"agestr"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self presentMessageTips:self.iupdatePetInfo.msg];
    }
}

ON_SIGNAL3(updatePetInfo, RELOADING, SIGNAL){
    [self presentLoadingTips:@"加载中"];
}

#pragma mark - Signal

ON_CREATE_VIEWS( signal )
{
    isEdit = NO;
    self.navigationBarShown = YES;
    self.title = @"宠物资料";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationBarLeft = [UIImage imageNamed:@"back.png"];
    rightbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightbutton.frame = CGRectMake(0, 0, 40, 40);
    [rightbutton addTarget:self action:@selector(statsChange) forControlEvents:UIControlEventTouchUpInside];
    [rightbutton setTitle:@"提交" forState:0];
    [rightbutton.titleLabel setFont:[UIFont systemFontOfSize:12]];
    [rightbutton setTitleColor:[UIColor whiteColor] forState:0];
    UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [v addSubview:rightbutton];
    self.navigationBarRight= v;
    
    self.list.lineCount=1;
    self.list.whenReloading=^{
        self.list.total = 1;
        for (BeeUIScrollItem *item in self.list.items) {
            item.clazz = [updatePetCell class];
            item.size = CGSizeMake(self.list.width, [UIScreen mainScreen].bounds.size.height);
            item.data = self.igetPetInfoByID.name;
        }
    };
    piker = [[CustomDatePiker alloc]init];
    [piker getAge:^(NSString  *agestr,NSInteger age){
        BeeUIScrollItem *item = self.list.items[0];
        updatePetCell * cell = (updatePetCell *)item.view;
        if (age>=12) {
            cell.petAgeLabel.text = [NSString stringWithFormat:@"%d岁%d个月",age/12,age%12];
        }else{
            cell.petAgeLabel.text = [NSString stringWithFormat:@"%ld个月",(long)age];
        }
        newage =age;
        self.iupdatePetInfo.age = agestr;
    }];
    self.iupdatePetHead.pet_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"pet_id"];
    
    self.igetPetInfoByID.p_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"pet_id"];
    [self.igetPetInfoByID firstPage];
}

ON_SIGNAL3(updatePetCell, petType, SIGNAL){
    ChoosePets *choose = [[ChoosePets alloc]init];
    [choose getPetModelBlock:^(PETMODEL *model){
        currentModel = model;
        BeeUIScrollItem *item = self.list.items[0];
        updatePetCell * cell = (updatePetCell *)item.view;
        cell.petTypeLabel.text = model.name;
    }];
    [self.navigationController pushViewController:choose animated:YES];
}

ON_SIGNAL3(updatePetCell, petAge, SIGNAL){
    [self.view addSubview:piker];
    [piker showPiker];
}

-(void)statsChange{
    isEdit = !isEdit;
    BeeUIScrollItem *item = self.list.items[0];
    updatePetCell * cell = (updatePetCell *)item.view;
    
    self.iupdatePetInfo.pet_id = [[NSUserDefaults standardUserDefaults]objectForKey:@"pet_id"];
    self.iupdatePetInfo.petname = cell.petName.text;
    self.iupdatePetInfo.signature = cell.petSign.text;
    if (self.iupdatePetInfo.age == nil) {
        self.iupdatePetInfo.age = [[NSUserDefaults standardUserDefaults]objectForKey:@"petagestr"];
    }
    if (cell.male.selected == YES) {
        self.iupdatePetInfo.gender = @"male";
    }else{
        self.iupdatePetInfo.gender = @"female";
    }
    self.iupdatePetInfo.variety = currentModel.index;
    [self.iupdatePetInfo firstPage];
}

- (void)pickImageFromAlbum{
    imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    imagePicker.allowsEditing = YES;
    [[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"title.png"] forBarMetrics:UIBarMetricsDefault];
    [self presentViewController:imagePicker animated:YES completion:^{
        
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera){
        
    }
    NSInteger imglength = [UIImageJPEGRepresentation(image, 1.0) length];
    NSData *small = UIImageJPEGRepresentation(image, (imglength %10)/imglength);
    UIImage *final = [Common imageWithImageSimple:[UIImage imageWithData:small] scaledToSize:CGSizeMake(180, 180)];
    [[NSUserDefaults  standardUserDefaults]setObject:UIImageJPEGRepresentation(final, 0.75) forKey:@"head_img"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    BeeUIScrollItem *item = self.list.items[0];
    updatePetCell * cell = (updatePetCell *)item.view;
    [cell.addPhoto setBackgroundImage:final forState:0];
    
    [imagePicker dismissViewControllerAnimated:YES completion:^{
        [self.iupdatePetHead firstPage];
    }];
    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:NULL];
    NSLog(@"cancel");
}
- (UIImage *)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}


-(void)reloadView{
    
//    BeeUIScrollItem *item = self.list.items[0];
//    updatePetCell * cell = (updatePetCell *)item.view;
    
}

ON_DELETE_VIEWS( signal )
{
}

ON_LAYOUT_VIEWS( signal )
{
}

ON_WILL_APPEAR( signal )
{
}

ON_DID_APPEAR( signal )
{
}

ON_WILL_DISAPPEAR( signal )
{
    
}

ON_DID_DISAPPEAR( signal )
{
}
ON_SIGNAL3(updatePetCell, addPhoto, signal){
    [self pickImageFromAlbum];
}

ON_SIGNAL3( BeeUINavigationBar, LEFT_TOUCHED, signal )
{
    [self.navigationController popViewControllerAnimated:YES];
}

ON_SIGNAL3( BeeUINavigationBar, RIGHT_TOUCHED, signal )
{
}

@end
